package cn.stylefeng.guns.modular.business.frequency.financetransport.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.financetransport.entity.FinanceTransport;
import cn.stylefeng.guns.modular.business.frequency.financetransport.mapper.FinanceTransportMapper;
import cn.stylefeng.guns.modular.business.frequency.financetransport.model.param.FinanceTransportParam;
import cn.stylefeng.guns.modular.business.frequency.financetransport.service.FinanceTransportService;
import cn.stylefeng.guns.modular.business.frequency.financetransport.enums.FinanceTransportExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单财务信息表 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:17
 */
@Service
public class FinanceTransportServiceImpl extends ServiceImpl<FinanceTransportMapper, FinanceTransport> implements FinanceTransportService {

    @Override
    public PageResult<FinanceTransport> page(FinanceTransportParam financeTransportParam) {

        // 构造条件
        LambdaQueryWrapper<FinanceTransport> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<FinanceTransport> list(FinanceTransportParam financeTransportParam) {

        // 构造条件
        LambdaQueryWrapper<FinanceTransport> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(FinanceTransportParam financeTransportParam) {

        // 将dto转为实体
        FinanceTransport financeTransport = new FinanceTransport();
        BeanUtil.copyProperties(financeTransportParam, financeTransport);

        this.save(financeTransport);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(FinanceTransportParam financeTransportParam) {
        this.removeById(financeTransportParam.getId());
    }

    @Override
    public void edit(FinanceTransportParam financeTransportParam) {

        // 根据id查询实体
        FinanceTransport financeTransport = this.queryFinanceTransport(financeTransportParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(financeTransportParam, financeTransport);

        this.updateById(financeTransport);
    }

    @Override
    public FinanceTransport detail(FinanceTransportParam financeTransportParam) {
        return this.queryFinanceTransport(financeTransportParam);
    }



    /**
     * 获取订单财务信息表
     *
     * @author yujie
     * @since 2023/05/09 17:17
     */
    private FinanceTransport queryFinanceTransport(FinanceTransportParam financeTransportParam) {
        FinanceTransport financeTransport = this.getById(financeTransportParam.getId());
        if (ObjectUtil.isEmpty(financeTransport)) {
            throw new ServiceException(FinanceTransportExceptionEnum.NOT_EXISTED);
        }
        return financeTransport;
    }
}